﻿using Helper;
using IssueTracker.CustomControls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using ViewModel;

namespace IssueTracker
{
    public partial class StickyForm : Form
    {
        stickyForm_Statistics objStat = null;
        /*draggable window*/
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        /*Rounded corner */
        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn
        (
            int nLeftRect, // x-coordinate of upper-left corner
            int nTopRect, // y-coordinate of upper-left corner
            int nRightRect, // x-coordinate of lower-right corner
            int nBottomRect, // y-coordinate of lower-right corner
            int nWidthEllipse, // height of ellipse
            int nHeightEllipse // width of ellipse
        );

        public Login objLogin;
        public MainWindow objMainWindow;
        public Member currentUser { get; set; }

        public StickyForm()
        {
            InitializeComponent();
            Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));


            int x = Screen.PrimaryScreen.WorkingArea.Width - this.Width - 20;
            int y = 20;//Screen.PrimaryScreen.WorkingArea.Height - this.Height;
            this.Location = new Point(x, y);
            SetStyle(ControlStyles.OptimizedDoubleBuffer |
                        ControlStyles.UserPaint |
                        ControlStyles.AllPaintingInWmPaint, true);
        }

        private void StickyForm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }

        private void StickyForm_Load(object sender, EventArgs e)
        {
            notifyIcon1.ContextMenuStrip = contextMenuStrip1;
            if (!notifyIcon1.Visible)
                notifyIcon1.Visible = true;
            notifyIcon1.Icon = IssueTracker.Properties.Resources.app_icon;
            notifyIcon1.ShowBalloonTip(3 * 1000);
            ShowInTaskbar = false;
            string username = IssueTracker.Properties.Settings.Default.username;
            string psw = IssueTracker.Properties.Settings.Default.password;


            try
            {
                ActiveSession.User = Service.Login.ValidateUserLogin(username, psw);
            }
            catch
            {

            }

            if (ActiveSession.User != null && ActiveSession.User.ID > 0)
            {
                LoadStatistics();
            }
            else
            {
                LoadLoginPanel();
            }

        }

        private void panelHead_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }

        public void LoadLoginPanel()
        {
            ActiveSession.User = null;
            panelBody.Controls.Clear();
            stickyForm_Login objLoginControl = new stickyForm_Login();
            objLoginControl.objStickyForm = this;
            objLoginControl.Dock = DockStyle.Fill;
            panelBody.Controls.Add(objLoginControl);
        }

        public void LoadStatistics()
        {
            Cursor.Current = Cursors.WaitCursor;
            if (objStat != null)
                objStat.StopPendingBackgroundOperation();
            panelBody.Controls.Clear();
            objStat = new stickyForm_Statistics();
            objStat.objStickyForm = this;
            objStat.Dock = DockStyle.Fill;
            panelBody.Controls.Add(objStat);
            Cursor.Current = Cursors.Default;
        }

        public void ShowLoginWindow()
        {
            if (objLogin == null)
            {
                objLogin = new Login();
                objLogin.objStickyForm = this;
                objLogin.Show();
            }
            else
                objLogin.BringToFront();
        }

        public void ShowMainWindow()
        {
            if (objMainWindow == null)
            {
                objMainWindow = new MainWindow();
                objMainWindow.objStickyForm = this;
                objMainWindow.currentUser = currentUser;
                objMainWindow.Show();
            }
            else
            {
                objMainWindow.BringToFront();
            }
        }

        public void CloseMainWindow()
        {
            objMainWindow.Close();
            objMainWindow = null;
        }

        public void CloseLoginWindow()
        {
            objLogin.Close();
            objLogin = null;
        }

        public void CloseApp()
        {
            objMainWindow.Close();
            objLogin.Close();
            objMainWindow = null;
            objLogin = null;
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            if (ActiveSession.User == null)
                ShowLoginWindow();
            else
                ShowMainWindow();
        }

        private void StickyForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            notifyIcon1.Visible = false;
            Service.Login.LogOut(ActiveSession.User.ID);
        }

        public void StopPendingBackgroundOperation()
        {
            objStat.StopPendingBackgroundOperation();
        }

        private void OpentoolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ActiveSession.User == null || ActiveSession.User.ID <= 0)
                ShowLoginWindow();
            else
                ShowMainWindow();
        }

        private void ExittoolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void LogOutoolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadLoginPanel();
            if (ActiveSession.User.ID > 0)
                Service.Login.LogOut(ActiveSession.User.ID);
        }
    }
}
